import { Stack, Construct } from '@aws-cdk/core';
import { Database, IDatabase, ITable, CfnTable } from '@aws-cdk/aws-glue';

import { config } from '../config';
import { s3BucketProps } from '../helpers/interfaces';

export class TestGlueStack extends Stack {

    public readonly gDB: IDatabase;

    constructor(scope: Construct, id: string, props: s3BucketProps) {
        super(scope, id, props);

        const testDatabase = new Database(this, 'testDB', {
            databaseName: config.databaseName
        });
        const empLoc = `s3://${props.s3EmpMaster.bucketName}/emp`;
        const empTable = new CfnTable(this, 'empMaster', {
            catalogId: testDatabase.catalogId,
            databaseName: testDatabase.databaseName,
            tableInput: {
                description: 'empMaster table generated by cdk',
                name: config.empMaster,
                parameters: {
                    'skip.header.line.count': '1',
                    'EXTERNAL': 'TRUE',
                    'has_encrypted_data': 'false',
                },
                tableType: 'EXTERNAL_TABLE',
                storageDescriptor: {
                    compressed: false,
                    columns: [
                        {
                          name: 'emp_id',
                          type: 'int'
                        },
                        {
                            name: 'emp_name',
                            type: 'string'
                        },
                        {
                            name: 'emp_email',
                            type: 'string'
                        },
                        {
                            name: 'emp_phone',
                            type: 'char(10)'
                        }
                    ],
                    inputFormat: 'org.apache.hadoop.mapred.TextInputFormat',
                    outputFormat: 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat',
                    serdeInfo: {
                        serializationLibrary: 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe',
                        parameters: {
                            'field.delim': '\t',
                            'serialization.format': '\t'
                        }
                    },
                    location: empLoc,
                    numberOfBuckets: -1,
                    storedAsSubDirectories: false
                },
            }
        });

        const empDetailsLoc = `s3://${props.s3EmpMaster.bucketName}/empDetails`;
        const empDetailTable = new CfnTable(this, 'empDetails', {
            catalogId: testDatabase.catalogId,
            databaseName: testDatabase.databaseName,
            tableInput: {
                description: 'empDetails table generated by cdk',
                name: config.empDetails,
                parameters: {
                    'skip.header.line.count': '1',
                    'EXTERNAL': 'TRUE',
                    'has_encrypted_data': 'false',
                },
                tableType: 'EXTERNAL_TABLE',
                storageDescriptor: {
                    compressed: false,
                    columns: [
                        {
                          name: 'emp_id',
                          type: 'int'
                        },
                        {
                            name: 'emp_name',
                            type: 'string'
                        },
                        {
                            name: 'emp_email',
                            type: 'string'
                        },
                        {
                            name: 'emp_phone',
                            type: 'char(10)'
                        }
                    ],
                    inputFormat: 'org.apache.hadoop.mapred.TextInputFormat',
                    outputFormat: 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat',
                    serdeInfo: {
                        serializationLibrary: 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe',
                        parameters: {
                            'field.delim': ',',
                            'serialization.format': ','
                        }
                    },
                    location: empDetailsLoc,
                    numberOfBuckets: -1,
                    storedAsSubDirectories: false
                },
            }
        });

        this.gDB = testDatabase;
    }
}